package com.dhcc.sdc.busi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.busi.dao.SdcHomeIndexMapper;
import com.dhcc.sdc.busi.service.ISdcHomeIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("sdcHomeIndexService")
public class SdcHomeIndexServiceImpl implements ISdcHomeIndexService {

    @Autowired
    private SdcHomeIndexMapper sdcHomeIndexMapper;
    @Override
    public JSONObject getHomeData(Map<String, Object> map) {
        Map<String, Object> homeData = sdcHomeIndexMapper.getHomeSumData(map);
        JSONObject caseObj = new JSONObject();
        //在院患者数
        caseObj.put("allCase", homeData.get("allCase"));
        caseObj.put("qcCase", homeData.get("qcCase"));
        caseObj.put("qcCaseNum", homeData.get("qcCaseNum"));
        caseObj.put("inCase", homeData.get("inCase"));
        caseObj.put("drgNum", homeData.get("drgNum"));
        //病种分布饼图
        JSONArray drgPieArray = new JSONArray();
        List<Map<String, Object>> drgList = sdcHomeIndexMapper.getDrgChartData(map);
        for (Map<String, Object> stringObjectMap : drgList) {
            drgPieArray.add(getJSONObj(2, "name", stringObjectMap.get("entityName").toString(), "value", stringObjectMap.get("caseNum").toString(),"",""));
        }
        caseObj.put("drgPieData", drgPieArray);
        //科室统计柱图
        JSONArray departArray = new JSONArray();
        List<Map<String, Object>> departList = sdcHomeIndexMapper.getDepartChartData(map);
        for (Map<String, Object> stringObjectMap : departList) {
            departArray.add(getJSONObj(2, "ddesc", stringObjectMap.get("departName").toString(), "caseNum", stringObjectMap.get("caseNum").toString(), "", ""));
        }
        caseObj.put("departBarData", departArray);
        //医生统计柱图
        JSONArray doctorArray = new JSONArray();
        List<Map<String, Object>> doctorList = sdcHomeIndexMapper.getDoctorChartData(map);
        for (Map<String, Object> stringObjectMap : doctorList) {
            doctorArray.add(getJSONObj(2, "ddesc", CommonUtil.isEmpty(stringObjectMap.get("doctorName"))?"":stringObjectMap.get("doctorName").toString(), "qcNum", stringObjectMap.get("qcNum").toString(), "", ""));
        }
        caseObj.put("doctorBarData", doctorArray);

        //患者统计柱图
        JSONArray patientArray = new JSONArray();
        List<Map<String, Object>> patientList = sdcHomeIndexMapper.getPatientChartData(map);
        for (Map<String, Object> stringObjectMap : patientList) {
            patientArray.add(getJSONObj(2, "ddesc", stringObjectMap.get("name").toString(), "qcNum", stringObjectMap.get("qcNum").toString(), "", ""));
        }
        caseObj.put("patientBarData", patientArray);
        return caseObj;
    }

    @Override
    public List<Map<String, Object>> selectAllDepartData(Map<String, Object> map) {
        return this.sdcHomeIndexMapper.selectAllDepartData(map);
    }

    @Override
    public List<Map<String, Object>> selectAllDoctorData(Map<String, Object> map) {
        return this.sdcHomeIndexMapper.selectAllDoctorData(map);
    }

    @Override
    public List<Map<String, Object>> getQcCaseList(Page<Map<String, Object>> page, Map<String, Object> params) {
        String key = (String)params.get("key");
        String sqlStr = getSqlStr(key, params);
        return this.sdcHomeIndexMapper.qcCaseList(sqlStr, page, params);
    }

    @Override
    public List<Map<String, Object>> getQcEntityList(Page<Map<String, Object>> page, Map<String, Object> params) {
        String key = (String)params.get("key");
        String sqlStr = getSqlStr(key, params);
        return this.sdcHomeIndexMapper.qcEntityList(sqlStr, page, params);
    }

    /**
     * 根据图例名称 返回查询SQL
     */
    public String getSqlStr(String key, Map<String, Object> params){
        String sqlStr = "";
        switch (key) {
            case "qcCase":
            case "qcCaseNum"://主页使用
                sqlStr = "and tr.id is not null";
                break;
            default:
                break;
        }
        return sqlStr;
    }

    /**
     * @Description: 组装JSONObject
     * @return: JSONObject
     * @author: wujiafeng
     * @date:2020年1月3日 上午11:32:54
     */
    public JSONObject getJSONObj(int keyNum,String key1,String value1,String key2,String value2,String key3,String value3){
        JSONObject jsonObj = new JSONObject();
        if(keyNum==2){
            jsonObj.put(key1, value1);
            jsonObj.put(key2, value2);
        }else if(keyNum==3){
            jsonObj.put(key1, value1);
            jsonObj.put(key2, value2);
            jsonObj.put(key3, value3);
        }
        return jsonObj;
    }

    /**
     * @Description: 查询医生质控数据列表
     * @return: 医生质控数据列表
     * @author: yanghangwei
     * @date:2023年8月10日 下午16:32:54
     */
    public List<Map<String, Object>> selectDoctorChartData(Map<String, Object> map){
        return sdcHomeIndexMapper.getDoctorChartData(map);
    }

    @Override
    public List<Map<String, Object>> getLeaveQcEntityList(Page<Map<String, Object>> page, Map<String, Object> params) {
        String key = (String)params.get("key");
        String sqlStr = getSqlStr(key, params);
//        return this.sdcHomeIndexMapper.qcEntityList(sqlStr, page, params);
        return this.sdcHomeIndexMapper.qcLeaveEntityList(sqlStr, page, params);
    }

    @Override
    public List<Map<String, Object>> selectLeaveAllDoctorData(Map<String, Object> map) {
        return this.sdcHomeIndexMapper.selectLeaveAllDoctorData(map);
    }

    @Override
    public List<Map<String, Object>> getLeaveQcCaseList(Page<Map<String, Object>> page, Map<String, Object> params) {
        String key = (String)params.get("key");
        String sqlStr = getSqlStr(key, params);
//        return this.sdcHomeIndexMapper.qcCaseList(sqlStr, page, params);
        return this.sdcHomeIndexMapper.qcLeaveCaseList(sqlStr, page, params);
    }

    @Override
    public List<Map<String, Object>> selectLeaveAllDepartData(Map<String, Object> map) {
        List<Map<String, Object>> maps = this.sdcHomeIndexMapper.selectLeaveAllDepartData(map);
        return maps;
    }

    @Override
    public JSONObject getLeaveHomeData(Map<String, Object> map) {
        Map<String, Object> homeData = sdcHomeIndexMapper.getLeaveHomeSumData(map);
        JSONObject caseObj = new JSONObject();
        //出院患者数
        caseObj.put("allCase", homeData.get("allCase"));
        caseObj.put("qcCase", homeData.get("qcCase"));
        caseObj.put("qcCaseNum", homeData.get("qcCaseNum"));
        caseObj.put("inCase", homeData.get("inCase"));
        caseObj.put("drgNum", homeData.get("drgNum"));
        //病种分布饼图
        JSONArray drgPieArray = new JSONArray();
        List<Map<String, Object>> drgList = sdcHomeIndexMapper.getLeaveDrgChartData(map);
        for (Map<String, Object> stringObjectMap : drgList) {
            drgPieArray.add(getJSONObj(2, "name", stringObjectMap.get("entityName").toString(), "value", stringObjectMap.get("caseNum").toString(),"",""));
        }
        caseObj.put("drgPieData", drgPieArray);
        //科室统计柱图
        JSONArray departArray = new JSONArray();
        List<Map<String, Object>> departList = sdcHomeIndexMapper.getLeaveDepartChartData(map);
        for (Map<String, Object> stringObjectMap : departList) {
            departArray.add(getJSONObj(2, "ddesc", stringObjectMap.get("departName").toString(), "caseNum", stringObjectMap.get("caseNum").toString(), "", ""));
        }
        caseObj.put("departBarData", departArray);
        //医生统计柱图
        JSONArray doctorArray = new JSONArray();
        List<Map<String, Object>> doctorList = sdcHomeIndexMapper.getLeaveDoctorChartData(map);
        for (Map<String, Object> stringObjectMap : doctorList) {
            doctorArray.add(getJSONObj(2, "ddesc", CommonUtil.isEmpty(stringObjectMap.get("doctorName"))?"":stringObjectMap.get("doctorName").toString(), "qcNum", stringObjectMap.get("qcNum").toString(), "", ""));
        }
        caseObj.put("doctorBarData", doctorArray);

        //患者统计柱图
        JSONArray patientArray = new JSONArray();
        List<Map<String, Object>> patientList = sdcHomeIndexMapper.getLeavePatientChartData(map);
        for (Map<String, Object> stringObjectMap : patientList) {
            patientArray.add(getJSONObj(2, "ddesc", stringObjectMap.get("name").toString(), "qcNum", stringObjectMap.get("qcNum").toString(), "", ""));
        }
        caseObj.put("patientBarData", patientArray);
        return caseObj;
    }

}
